package Server.Poker.NTPDK;

public class AIPlayer extends PlayerBase {

	int m_cbTurnCardCount; // 出牌数目
	int m_cbTurnCardData[] = new int[Constants.MAX_COUNT]; // 出牌列表
	int m_wOutCardUser; // 出牌玩家
	int m_wBankerUser; // 地主玩家

	// 手上扑克
	int m_cbHandCardCount; // 扑克数目
	int m_cbHandCardData[] = new int[Constants.MAX_COUNT]; // 手上扑克
	int m_cbCurrentLandScore; // 已叫分数
	int m_bBackCard[] = new int[Constants.BACK_COUNT]; // 底牌扑克
	int m_bMingCard; // 明牌扑克

	// 控件变量
	PDKLogic_Server m_GameLogic = new PDKLogic_Server(); // 游戏逻辑

	//////////////////////////////////////////////////////////////////////////
	public AIPlayer() {
		// 扑克变量
		m_cbTurnCardCount = 0;
		Constants.ZeroMemory(m_cbTurnCardData);
		m_wOutCardUser = Constants.INVALID_CHAIR;
		m_wBankerUser = Constants.INVALID_CHAIR;

		// 手上扑克
		m_cbHandCardCount = 0;
		Constants.ZeroMemory(m_cbHandCardData);
		m_cbCurrentLandScore = 255;
	}

	boolean OnSubSendCard(Object pData, int wDataSize) {

		// 变量定义
		CMD_S_SendAllCard pSendCard = (CMD_S_SendAllCard) pData;

		// 设置状态
		SetGameStatus(Constants.GS_WK_SCORE);

		// 扑克变量
		m_cbTurnCardCount = 0;
		Constants.ZeroMemory(m_cbTurnCardData);
		m_cbCurrentLandScore = 255;

		// 所有扑克
		for (int wChairID = 0; wChairID < Constants.DDZ_PLAYER; ++wChairID)
			m_GameLogic.SetUserCard(wChairID, pSendCard.bCardData[wChairID], Constants.NORMAL_COUNT);

		// 叫牌扑克
		int cbLandScoreCardData[] = new int[Constants.MAX_COUNT];
		Constants.CopyMemory(cbLandScoreCardData, pSendCard.bCardData[GetChairID()], Constants.NORMAL_COUNT);
		Constants.CopyMemory(cbLandScoreCardData, Constants.NORMAL_COUNT, pSendCard.bBackCardData,
				Constants.BACK_COUNT);
		m_GameLogic.SetLandScoreCardData(cbLandScoreCardData, Constants.sizeof(cbLandScoreCardData));
		Constants.CopyMemory(m_bBackCard, pSendCard.bBackCardData, Constants.BACK_COUNT);

		// 手上扑克
		m_cbHandCardCount = Constants.NORMAL_COUNT;
		Constants.CopyMemory(m_cbHandCardData, pSendCard.bCardData[GetChairID()], Constants.NORMAL_COUNT);

		// 排列扑克
		m_GameLogic.SortCardList(m_cbHandCardData, m_cbHandCardCount, Constants.ST_ORDER);

		// 玩家处理
		if (GetChairID() == pSendCard.wCurrentUser) {
			int nElapse = Constants.rand() % Constants.TIME_CALL_SCORE + Constants.TIME_LESS;
			SetTimer(Constants.IDI_CALL_SCORE, nElapse + Constants.TIME_DISPATCH);
		}

		return true;
	}

	boolean OnSubLandScore(Object pData, int wDataSize) {

		// 变量定义
		CMD_S_LandScore pCallScore = (CMD_S_LandScore) pData;
		m_cbCurrentLandScore = pCallScore.bCurrentScore;

		// 设置庄家
		m_wBankerUser = pCallScore.wLandUser;
		m_GameLogic.SetBanker(pCallScore.wLandUser);

		// 用户处理
		if (GetChairID() == pCallScore.wCurrentUser) {
			int nElapse = Constants.rand() % Constants.TIME_CALL_SCORE + Constants.TIME_LESS;
			SetTimer(Constants.IDI_CALL_SCORE, nElapse);
		}

		return true;
	}

	boolean OnSubGameStart(Object pData, int wDataSize) {

		// 变量定义
		CMD_S_GameStart pGameStart = (CMD_S_GameStart) pData;

		// 设置状态
		SetGameStatus(Constants.GS_WK_PLAYING);

		// 设置庄家
		m_wBankerUser = pGameStart.wLandUser;
		m_GameLogic.SetBanker(pGameStart.wLandUser);

		// 设置底牌
		m_GameLogic.SetBackCard(pGameStart.wLandUser, pGameStart.bBackCard, Constants.BACK_COUNT);

		// 设置扑克
		if (pGameStart.wLandUser == GetChairID()) {
			// 设置扑克
			m_cbHandCardCount += Constants.CountArray(pGameStart.bBackCard);
			Constants.CopyMemory(m_cbHandCardData,Constants.NORMAL_COUNT, pGameStart.bBackCard, Constants.sizeof(pGameStart.bBackCard));

			// 排列扑克
			m_GameLogic.SortCardList(m_cbHandCardData, m_cbHandCardCount, Constants.ST_ORDER);
		}

		// 玩家设置
		if (pGameStart.wCurrentUser == GetChairID()) {
			int nElapse = Constants.rand() % Constants.TIME_AI_OUT_CARD + Constants.TIME_LESS;
			SetTimer(Constants.IDI_OUT_CARD, nElapse);
		}
		return true;
	}

	boolean OnSubOutCard(Object pData, int wDataSize) {
		// 变量定义
		CMD_S_OutCard pOutCard = (CMD_S_OutCard) pData;

		// 出牌变量
		if (pOutCard.wCurrentUser == pOutCard.wOutCardUser) {
			m_cbTurnCardCount = 0;
			Constants.ZeroMemory(m_cbTurnCardData);
		} else {
			m_cbTurnCardCount = pOutCard.bCardCount;
			m_wOutCardUser = pOutCard.wOutCardUser;
			Constants.CopyMemory(m_cbTurnCardData, pOutCard.bCardData, pOutCard.bCardCount);
		}

		// 玩家设置
		if (GetChairID() == pOutCard.wCurrentUser) {
			int nElapse = Constants.rand() % Constants.TIME_OUT_CARD + Constants.TIME_LESS;
			SetTimer(Constants.IDI_OUT_CARD, nElapse);
		}

		// 设置变量
		m_GameLogic.RemoveUserCardData(pOutCard.wOutCardUser, pOutCard.bCardData, pOutCard.bCardCount);

		return true;
	}

	boolean OnSubPassCard(Object pData, int wDataSize) {
		// 变量定义
		CMD_S_PassCard pPassCard = (CMD_S_PassCard) pData;

		// 一轮判断
		if (pPassCard.bNewTurn == 1) {
			m_cbTurnCardCount = 0;
			Constants.ZeroMemory(m_cbTurnCardData);
		}

		// 玩家设置
		if (GetChairID() == pPassCard.wCurrentUser) {
			int nElapse = Constants.rand() % Constants.TIME_OUT_CARD + Constants.TIME_LESS;
			SetTimer(Constants.IDI_OUT_CARD, nElapse);
		}

		return true;
	}

	boolean OnSubGameEnd(Object pData, int wDataSize) {

		// 变量定义
		CMD_S_GameEnd pGameEnd = (CMD_S_GameEnd) pData;

		// 设置状态
		SetGameStatus(Constants.GS_WK_FREE);

		// 设置变量
		m_cbTurnCardCount = 0;
		m_cbHandCardCount = 0;
		Constants.ZeroMemory(m_cbTurnCardData);
		Constants.ZeroMemory(m_cbHandCardData);

		// 删除时间
		KillTimer(Constants.IDI_OUT_CARD);
		KillTimer(Constants.IDI_CALL_SCORE);

		// 开始设置
		int nElapse = Constants.rand() % Constants.TIME_START_GAME + Constants.TIME_LESS;
		SetTimer(Constants.IDI_START_GAME, nElapse);

		return true;
	}

	boolean OnSubTrustee(Object pBuffer, int wDataSize) {
		return true;
	}

	// AIPLAYER的状态机
	// 在场景为GS_WK_FREE的时候，会启动IDI_START_GAME的定时器，这个定时器通知桌子已开始
	//
	void OnTimer(int nIDEvent) {
		switch (nIDEvent) {
		case Constants.IDI_START_GAME: // 开始游戏
		{
			// 开始判断
			SendUserReady(null, 0);

			return;
		}
		case Constants.IDI_CALL_SCORE: // 用户叫分
		{
			// 构造变量
			CMD_C_LandScore CallScore = new CMD_C_LandScore();

			// 设置变量
			CallScore.bLandScore = m_GameLogic.LandScore(GetChairID(), m_cbCurrentLandScore);

			// 发送数据
			SendSocketData(Constants.SUB_C_LAND_SCORE, CallScore);

			return;
		}
		case Constants.IDI_OUT_CARD: // 用户出牌
		{
			// 扑克分析
			tagOutCardResult_Server OutCardResult = new tagOutCardResult_Server();
			try {
				m_GameLogic.SearchOutCard(m_cbHandCardData, m_cbHandCardCount, m_cbTurnCardData, m_cbTurnCardCount,m_wOutCardUser, GetChairID(), OutCardResult);
			} catch (Exception e) {
				e.printStackTrace();
				// 这里的设置，使得进入下面的if处理
				Constants.ZeroMemory(OutCardResult.cbResultCard);
				OutCardResult.cbCardCount = 10;
				
			}
			// 牌型合法判断
			if (OutCardResult.cbCardCount > 0 
					&& 
					Constants.CT_ERROR == m_GameLogic.GetCardType(OutCardResult.cbResultCard, OutCardResult.cbCardCount)) {
				OutCardResult = new tagOutCardResult_Server();
				System.out.println("xxxx");
			}

			// 先出牌不能为空
			if (m_cbTurnCardCount == 0) {
				if (OutCardResult.cbCardCount == 0) {
					// 最小一张
					OutCardResult.cbCardCount = 1;
					OutCardResult.cbResultCard[0] = m_cbHandCardData[m_cbHandCardCount - 1];
				}
			}

			// 结果判断
			if (OutCardResult.cbCardCount > 0) {
				// 合法判断
				if (m_cbTurnCardCount > 0 && !m_GameLogic.CompareCard(m_cbTurnCardData, OutCardResult.cbResultCard,
						m_cbTurnCardCount, OutCardResult.cbCardCount)) {
					// 放弃出牌
					SendSocketData(Constants.SUB_C_PASS_CARD);
					return;
				}

				// 删除扑克
				m_cbHandCardCount -= OutCardResult.cbCardCount;
				m_GameLogic.RemoveCard(OutCardResult.cbResultCard, OutCardResult.cbCardCount, m_cbHandCardData,
						m_cbHandCardCount + OutCardResult.cbCardCount);

				// 构造变量
				CMD_C_OutCard OutCard = new CMD_C_OutCard();

				// 设置变量
				OutCard.bCardCount = OutCardResult.cbCardCount;
				Constants.CopyMemory(OutCard.bCardData, OutCardResult.cbResultCard, OutCardResult.cbCardCount);

				// 发送数据
				SendSocketData(Constants.SUB_C_OUT_CART, OutCard);
			} else {
				// 放弃出牌
				SendSocketData(Constants.SUB_C_PASS_CARD);
			}

			return;
		}
		}

		return;
	}
	// 场景消息
	boolean OnEventGameScene(int cbGameStatus, boolean bLookonOther, Object pData, int wDataSize) {
		switch (cbGameStatus) {
		case Constants.GS_WK_FREE: // 空闲状态
		{

			int nElapse = Constants.rand() % Constants.TIME_START_GAME + Constants.TIME_LESS;
			SetTimer(Constants.IDI_START_GAME, nElapse);

			return true;
		}
		case Constants.GS_WK_SCORE: // 叫分状态
		{
			CMD_S_StatusScore pStatusCall = (CMD_S_StatusScore) pData;

			// 扑克数据
			m_cbHandCardCount = Constants.NORMAL_COUNT;
			Constants.CopyMemory(m_cbHandCardData, pStatusCall.bCardData, m_cbHandCardCount);

			// 叫分设置
			if (GetChairID() == pStatusCall.wCurrentUser) {
				int nElapse = Constants.rand() % Constants.TIME_CALL_SCORE + Constants.TIME_LESS;
				SetTimer(Constants.IDI_CALL_SCORE, nElapse + Constants.TIME_DISPATCH);
			}

			return true;
		}
		case Constants.GS_WK_PLAYING: // 游戏状态
		{
			CMD_S_StatusPlay pStatusPlay = (CMD_S_StatusPlay) pData;

			// 出牌变量
			m_cbTurnCardCount = pStatusPlay.bTurnCardCount;
			Constants.CopyMemory(m_cbTurnCardData, pStatusPlay.bTurnCardData);

			// 扑克数据
			int wMeChairID = GetChairID();
			m_cbHandCardCount = pStatusPlay.bCardCount[wMeChairID];
			Constants.CopyMemory(m_cbHandCardData, pStatusPlay.bCardData, m_cbHandCardCount);

			// 玩家设置
			if (pStatusPlay.wCurrentUser == GetChairID()) {
				int nElapse = Constants.rand() % Constants.TIME_OUT_CARD + Constants.TIME_LESS;
				SetTimer(Constants.IDI_OUT_CARD, nElapse);
			}

			return true;
		}
		}

		return false;
	}

	boolean RepositUserItemSink() {
		// 扑克变量
		m_cbTurnCardCount = 0;
		Constants.ZeroMemory(m_cbTurnCardData);

		// 手上扑克
		m_cbHandCardCount = 0;
		Constants.ZeroMemory(m_cbHandCardData);

		return true;
	}

	public void OnGameBegin() {
		RepositUserItemSink();
	}

	public void OnGameEnd() {
		RepositUserItemSink();
	}

	@Override
	boolean ProcessRecvdPacket(int dwMaxPacketSize) {
		return false;
	}

	
	
	

}
